<!DOCTYPE HTML>
<html lang="en">
<head>
<title>SubStr() - Syntax &amp; Usage | AutoHotkey</title>
<meta name="description" content="The SubStr function retrieves one or more characters from the specified position in a string." />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
</head>
<body>

<h1>SubStr() <span class="ver">[v1.0.46+]</span></h1>

<p>Retrieves one or more characters from the specified position in a string.</p>

<pre class="Syntax">NewStr := <span class="func">SubStr</span>(String, StartingPos <span class="optional">, Length</span>)</pre>
<h2>Parameters</h2>
<dl>

  <dt>String</dt>
  <dd><p>The string whose content is copied.</p></dd>

  <dt>StartingPos</dt>
  <dd><p>Specify 1 to start at the first character, 2 to start at the second, and so on (if <em>StartingPos</em> is beyond <em>String</em>'s length, an empty string is returned). If <em>StartingPos</em> is less than 1, it is considered to be an offset from the end of the string. For example, 0 extracts the last character and -1 extracts the two last characters (but if <em>StartingPos</em> tries to go beyond the left end of the string, the extraction starts at the first character).</p></dd>

  <dt>Length</dt>
  <dd><p>If this parameter is omitted, it defaults to "all characters". Otherwise, specify the maximum number of characters to retrieve (fewer than the maximum are retrieved whenever the remaining part of the string is too short). You can also specify a negative <em>Length</em> to omit that many characters from the end of the returned string (an empty string is returned if all or too many characters are omitted).</p></dd>

</dl>

<h2>Return Value</h2>
<p>This function returns the requested substring of <em>String</em>.</p>

<h2>Remarks</h2>
<p>Functionally, the <strong>SubStr</strong> function is almost the same as the <a href="StringMid.htm">StringMid</a> command. However, it's recommended to use SubStr, because it is more flexible and future-proofed than StringMid.</p>

<h2>Related</h2>
<p><a href="RegExMatch.htm">RegExMatch()</a>, <a href="StringMid.htm">StringMid</a>, <a href="StringLeft.htm">StringLeft/Right</a>, <a href="StringTrimLeft.htm">StringTrimLeft/Right</a></p>

<h2>Examples</h2>
<div class="ex" id="ExBasic">
<p><a href="#ExBasic">#1</a></p>
<pre>MsgBox % SubStr("123abc789", 4, 3) <em>; Returns abc</em></pre>
</div>

<div class="ex" id="ExEnd">
<p><a href="#ExEnd">#2</a></p>
<pre>String := "The Quick Brown Fox Jumps Over the Lazy Dog"
MsgBox % SubStr(String, 1, 19)  <em>; Returns "The Quick Brown Fox"</em>
MsgBox % SubStr(String, -7)  <em>; Returns "Lazy Dog"</em>
</pre>
</div>

</body>
</html>
